package jhunter.web.mvc.controller;

import java.util.Map;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;

import common.service.EasyFormDataService;

import jhunter.dao.bo.BaseObject;
import jhunter.rt.conf.ext.ActionExt;
import jhunter.rt.conf.ext.ActionsExt;
import jhunter.rt.conf.ext.AppExt;
import jhunter.rt.conf.ext.ModuleExt;
import jhunter.web.mvc.ActionContext;

public abstract class BaseController implements Controller {

	private ActionContext context;

	private ActionExt action;

	private Map<String, Object> params;

	private Map<String, Object> session;

	private Map<String, Object> flash;

	private Map<String, Object> attributes;

	private ModuleExt moduleExt;

	public AppExt getAppExt() {
		AppExt appext = (AppExt) this.getAction().getActionNode().getParent().getParent().getParent().getObject();
		return appext;
	}

	public EasyFormDataService getEasyFormDataService() {
		EasyFormDataService service = getAppExt().getAppSpringContext().getBean(EasyFormDataService.class);
		return service;
	}

	public ModuleExt getModuleExt() {
		return moduleExt;
	}

	public void setModuleExt(ModuleExt moduleExt) {
		this.moduleExt = moduleExt;
	}

	public ActionContext getContext() {
		return context;
	}

	public void setContext(ActionContext context) {
		this.context = context;
		init();
	}

	protected void init() {
		params = context.getParameters();
		session = context.getSession();
		flash = context.getFlash();
		attributes = context.getAttributes();
		attributes.put("actionURI", getContextPath());
	}

	public Map<String, Object> getParams() {
		return params;
	}

	public void setParams(Map<String, Object> params) {
		this.params = params;
	}

	public Map<String, Object> getSession() {
		return session;
	}

	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public Map<String, Object> getFlash() {
		return flash;
	}

	public void setFlash(Map<String, Object> flash) {
		this.flash = flash;
	}

	public Subject getUser() {
		return SecurityUtils.getSubject();
	}

	@Override
	public ActionExt getAction() {
		return action;
	}

	public void setAction(ActionExt action) {
		this.action = action;
	}

	/**
	 * 
	 * @param name
	 * @return url
	 */
	public String getActionPath(String name) {
		ActionsExt aes = (ActionsExt) this.getAction().getActionNode().getParent().getObject();
		String path = aes.getAction(name).getActionNode().getPathInfo().getPath();
		return path;
	}

	public String getContextPath() {
		return this.getAction().getActionNode().getPathInfo().getPath();
	}

	public String rAction(String name) {
		ActionsExt aes = (ActionsExt) this.getAction().getActionNode().getParent().getObject();
		String path = aes.getAction(name).getActionNode().getPathInfo().getPath();
		return "r:" + path;
	}

	public String getActionPath(String moduleId, String actionName) {
		AppExt appext = (AppExt) this.action.getActionNode().getParent().getParent().getParent().getObject();
		ModuleExt me = appext.getModuleExt(moduleId);
		String path = me.getActionByName(actionName).getActionNode().getPathInfo().getPath();
		return path ;
	}
	
	public String rAction(String moduleId, String actionName) {
		return "r:" + getActionPath(moduleId, actionName);
	}

	public String form() {
		return form(this.action.getAction().getName());
	}

	public String form(String easyFormId) {
		return "form:" + easyFormId;
	}

	public Map<String, Object> getAttributes() {
		return attributes;
	}

	public void setAttributes(Map<String, Object> attributes) {
		this.attributes = attributes;
	}
	
	/****************************************交互 start****************************************************************/
	/**
	 * var p = parent; var iframe = p.window.frames['main-content']; if (iframe)
	 * {p = iframe;}; window.frameElement.trigger('close'); p.refresh();
	 * 
	 * @return
	 */
	public String close() {
		StringBuilder sb = new StringBuilder("@:");
		sb.append("<script>");
		sb.append("	var p = parent;");
		sb.append("	var iframe = p.window.frames['main-content'];");
		sb.append("	if (iframe) {p = iframe;};");
		sb.append("	p.window.refreshGrid();");
		sb.append("	window.frameElement.trigger('close');");
		sb.append("</script>");
		return sb.toString();
	}
	
	public String success() {
		return "@:success";
	}
	
	/****************************************交互 end****************************************************************/
	
	/*********************************** CRUD start *******************************************************/
	public void deleteRecord() {
		getEasyFormDataService().delete(ActionContext.getContext());
	}

	public void createRecord() {
		getEasyFormDataService().add(ActionContext.getContext());
	}

	public void initEditRecord() {
		BaseObject bo = getEasyFormDataService().findById(ActionContext.getContext());
		getAttributes().put("bizObject", bo);
	}

	public void editRecord() {
		getEasyFormDataService().update(ActionContext.getContext());
	}

	public String jsonListRecord() {
		String json = getEasyFormDataService().getJsonData(ActionContext.getContext());
		return "@:" + json;
	}

	/*********************************** CRUD end *******************************************************/

}
